{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Getting started with Guardrails AI\n",
    "\n",
    "Guardrails AI is a framework that helps you validate LLM outputs using advanced validation techniques (i.e. `validators`). It is designed to be easy to use and to integrate with your existing workflows.\n",
    "\n",
    "In order to use Guardrails AI, you need to download the validators you want to use and install them in your environment. You can find the list of available validators in the [Guardrails Hub](https://hub.guardrailsai.com).\n",
    "\n",
    "This notebook is a quick introduction to the Guardrails AI platform. We will cover the following topics:\n",
    "1. [Installation](#Installation)\n",
    "2. [Download a Validator](#Download-a-Validator)\n",
    "3. [Create a Guard for String Validation](#Create-a-Guard-for-String-Validation)\n",
    "4. [Create a Guard for JSON Validation](#Create-a-Guard-for-JSON-Validation)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation\n",
    "\n",
    "To get started, install the `guardrails` package using pip. For more information, see the [installation guide](https://docs.guardrails.ai/getting-started/installation)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install guardrails_ai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup validators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!guardrails hub install hub://guardrails/regex_match"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create and run `Guard`s\n",
    "\n",
    "A `Guard` is a Guardrails primitive that allows prompting and validation of strings and structured data. You can use a `Guard` to generate strings and validate them against a set of guardrails, or you can use a `Guard` to generate structured data and ensure that it satisfies a set of guardrails.\n",
    "\n",
    "|  | Strings | Structured Data |\n",
    "| --- | --- | --- |\n",
    "| Prompt | ✅ | ✅ |\n",
    "| Validate | ✅ | ✅ |\n",
    "\n",
    "You can use a `Guard` for one or more of the above tasks."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating a `Guard` for String Validation\n",
    "\n",
    "Below, you can create a `Guard` that prompts an LLM for a string output, and validates that output to see if it follows the expected regex pattern."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guardrails import Guard\n",
    "from guardrails.hub import RegexMatch\n",
    "\n",
    "# Setting up a guard that prompts an LLM to generate a puppy name and validates\n",
    "# that the name is a single capitalized word.\n",
    "string_guard = Guard.from_string(\n",
    "    prompt=\"Generate a puppy name\",\n",
    "    validators=[RegexMatch(pattern=\"^[A-Z][a-z]*$\")]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Using the guard for prompting and validation\n",
    "\n",
    "In order to prompt an LLM for a string output, pass in the LLM callable to the `__call__` method of the `Guard` object. The `Guard` object will then prompt the LLM for a string output, and validate it against the provided regex pattern."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "\n",
    "# string_guard calls the OpenAI API with the prompt and validates the response\n",
    "raw_llm_output, validated_llm_response, *rest = string_guard(openai.chat.completions.create)\n",
    "\n",
    "print(f\"Raw LLM output: {raw_llm_output}\")\n",
    "print(f\"Validated LLM response: {validated_llm_response}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Using the guard for validation only\n",
    "\n",
    "In order to validate an LLM output only (i.e. without prompting the LLM for a string output), pass in the LLM output to the `validate` method of the `Guard` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_output = \"Caesar\"  # Replace with the output of your LLM\n",
    "validation_outcome = string_guard.validate(llm_output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating a `Guard` for JSONs\n",
    "\n",
    "\n",
    "Below, you can create a `Guard` that prompts an LLM for a string output, and validates that output to see if it follows the expected regex pattern."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gd-base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
